package com.pig4cloud.pig.module.controller.survey;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.module.api.entity.estate.Estate;
import com.pig4cloud.pig.module.api.entity.property.Property;
import com.pig4cloud.pig.module.api.entity.survey.Survey;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.estate.IEstateService;
import com.pig4cloud.pig.module.service.property.IPropertyService;
import com.pig4cloud.pig.module.service.survey.ISurveyService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 问卷调查Controller
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Validated
@Api(value = "问卷调查控制器", tags = {"问卷调查管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/survey")
public class SurveyController {

    private final ISurveyService surveyService;
    private final IPropertyService propertyService;
	private final IEstateService estateService;
	private final IWxUserService wxUserService;

    /**
     * 查询问卷调查列表
     */
    @ApiOperation("查询问卷调查列表")
    @PreAuthorize("@pms.hasPermission('module:survey:list')")
    @GetMapping("/list")
    public TableDataInfo<Survey> list(@Validated Survey survey) {
        PigUser sysUser = SecurityUtils.getUser();
        QueryWrapper<Survey> qw = new QueryWrapper<>();
		if(sysUser.getUserType() == 2 || sysUser.getUserType() == 3){//物业公司
			qw.eq("property_id",sysUser.getPropertyId());
		}

		if(survey.getSurveyType()!=null){
			qw.eq("survey_type",survey.getSurveyType());
		}

		qw.orderByDesc("id");
        TableDataInfo<Survey> td = PageUtils.buildDataInfo(surveyService.page(PageUtils.buildPage(),qw));
        return td;
    }

    /**
     * 导出问卷调查列表
     */
    @ApiOperation("导出问卷调查列表")
    @PreAuthorize("@pms.hasPermission('module:survey:export')")
    @SysLog("问卷调查")
    @GetMapping("/export")
    public List export(@Validated Survey survey) {
        List<Survey> list = surveyService.list();
        // ExcelUtil<Survey> util = new // ExcelUtil<Survey>(Survey.class);
        return  list;// util.exportExcel(list, "问卷调查");
    }

    /**
     * 获取问卷调查详细信息
     */
    @ApiOperation("获取问卷调查详细信息")
    @PreAuthorize("@pms.hasPermission('module:survey:query')")
    @GetMapping("/{id}")
    public R<Survey> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        Survey survey = surveyService.getById(id);
        if(survey != null){
			WxUser wxUser = wxUserService.findById(survey.getFromWxUserId());
			if(wxUser!=null){
				if(StrUtil.isNotEmpty(wxUser.getNickName())){
					survey.setNickName(EmojiParser.parseToUnicode(wxUser.getNickName()));
				}
			}
		}
        return R.ok(survey);
    }

    /**
     * 新增问卷调查
     */
    @ApiOperation("新增问卷调查")
    @PreAuthorize("@pms.hasPermission('module:survey:add')")
    @SysLog("问卷调查")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody Survey survey) {
    	if(survey.getPropertyId()!=null){
    		Property property = propertyService.findById(survey.getPropertyId());
    		if(property!=null){
				survey.setPropertyName(property.getPropertyName());
			}
		}
		if(survey.getEstateId()!=null){
			Estate estate = estateService.findById(survey.getEstateId());
			if(estate!=null){
				survey.setEstateName(estate.getEstateName());
			}
		}
        return R.okByRow(surveyService.save(survey) ? 1 : 0);
    }

    /**
     * 修改问卷调查
     */
    @ApiOperation("修改问卷调查")
    @PreAuthorize("@pms.hasPermission('module:survey:edit')")
    @SysLog("问卷调查")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody Survey survey) {

		if(survey.getPropertyId()!=null){
			Property property = propertyService.findById(survey.getPropertyId());
			if(property!=null){
				survey.setPropertyName(property.getPropertyName());
			}
		}
		if(survey.getEstateId()!=null){
			Estate estate = estateService.findById(survey.getEstateId());
			if(estate!=null){
				survey.setEstateName(estate.getEstateName());
			}
		}
        return R.okByRow(surveyService.updateById(survey) ? 1 : 0);
    }

    /**
     * 删除问卷调查
     */
    @ApiOperation("删除问卷调查")
    @PreAuthorize("@pms.hasPermission('module:survey:remove')")
    @SysLog("问卷调查")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(surveyService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
